<?php
    include '../classes/fileupload.class.php';
    include '../classes/image.class.php';
    include 'header.php';
    include '../function.inc.php';

    if(isset($_GET['action']) && isset($_GET['action']) == 'modify') {


        $query = "select * from books where id='{$_GET['id']}'";
        // 多余的空格会导致无法查询
        $result = $db->query($query);
        $num_results=$result->num_rows;

        if($num_results > 0) {
            $row = $result->fetch_assoc();
        } else {
            echo "no data!";
        }
    }

    // 修改数据库中的数据
    if(isset($_POST['submit'])){

        // 创建变量
        $bookname=$_POST['bookname'];
        $publisher=$_POST['publisher'];
        $author=$_POST['author'];
        $price=$_POST['price'];
        // $pic=$_POST['pic'];
        $detail=$_POST['detail'];
        $ptime=time();

        // if(!$bookname || !$publisher || !$author || !$price) {
        //     echo "您未填写内容，请返回重试";
        //     exit;
        // }
        if(!get_magic_quotes_gpc()) {
            $bookname=addslashes($bookname);
            $publisher=addslashes($publisher);
            $author=addslashes($author);
            $price=doubleval($price);
            $detail=addslashes($detail);
        }
        // 如果用户有添加图片的动作，则上传图片
        if($_FILES['pic']['error'] == 0) {
            // 上传图片
            $pic = upload("pic");
            // 删除旧图片
            unlink("../uploads/{$_POST['oldimg']}");
            unlink("../uploads/thumb_{$_POST['oldimg']}");


            if(!$pic) {
                echo "文件上传错误！";
            }
            $re_query = "update books set bookname='".$bookname."',publisher='".$publisher."',author='".$author."',
            price='".$price."',ptime='".$ptime."',pic='".$pic."',detail='".$detail."' where id='{$_POST['id']}'";
        } else {
            $re_query = "update books set bookname='".$bookname."',publisher='".$publisher."',author='".$author."',
            price='".$price."',ptime='".$ptime."',detail='".$detail."' where id='{$_POST['id']}'";
        }

        $result=$db->query($re_query);
        if($result){
            echo $db->affected_rows."book updated into database";
        } else {
            echo "an error has occurred.the item was not updated";
        }
    }
 ?>
<h3>修改图书</h3>
<form action="modify.php?action=modify&id=<?php echo $_GET['id'] ?>" method="post" enctype="multipart/form-data">
    <input type="hidden" name="id" value="<?php echo $row['id'] ?>">
    <p>图书名称：<input type="text" name="bookname" value="<?php echo $row['bookname'] ?>" ></p>
    <p>出版社： <input type="text" name="publisher" value="<?php echo $row['publisher'] ?>" ></p>
    <p>作者：<input type="text" name="author" value="<?php echo $row['author'] ?>" ></p>
    <p>价格：<input type="text" name="price" value="<?php echo $row['price'] ?>" ></p>
    <p>详细描述：<textarea cols="40" rows="5" name="detail" ><?php echo $row['detail'] ?></textarea>  </p>
    <p>图片：<input type="file" name="pic" value=""  ></p>
    <input type="hidden" name="oldimg" value="<?php echo $row['pic']?>">
    <p><img width="100" src="<?php echo "../uploads/".$row['pic'] ?>"></p>
    <input type="submit" name="submit" value="修改"></p>
</form>
<?php
    include 'footer.php';
 ?>
